//
//  TimeTool.m
//  fastSign
//
//  Created by 朱潮 on 14-5-17.
//  Copyright (c) 2014年 zhuchao. All rights reserved.
//

#import "TimeTool.h"

@implementation TimeTool

+ (TimeTool *)sharedInstance
{
    static dispatch_once_t pred;
    static TimeTool *sharedInstance = nil;

    dispatch_once(&pred, ^{
        sharedInstance = [[self alloc] init];
    });

    return sharedInstance;
}


+ (NSString *)formatFriendlyTime:(NSDate *)todate {
    NSDate *today = [NSDate date]; //当前时间
    NSTimeInterval time = [today timeIntervalSinceDate:todate];

    if(time < 60){
        return @"刚刚";
    }else if (time < 3600) {
        unsigned int unitFlag = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
        NSDateComponents *gap = [[NSCalendar currentCalendar] components:unitFlag fromDate:today toDate:todate options:0]; //计算时间差
        return [NSString stringWithFormat:@"%ld分钟前",  (long)(ABS([gap minute]))];
    }else if(time < 24*3600){
        unsigned int unitFlag = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
        NSDateComponents *gap = [[NSCalendar currentCalendar] components:unitFlag fromDate:today toDate:todate options:0]; //计算时间差
        return [NSString stringWithFormat:@"%ld小时前", (long)(ABS([gap hour]))];
    }else if (time < 48*3600){
        return [self formatDate:todate formatWith:@"昨天 HH:mm"];
    }else if (time < 365*24*3600){
        return [self formatDate:todate formatWith:@"MM月dd日 HH:mm"];
    }else{
        return [self formatDate:todate formatWith:@"yyyy年MM月dd日 HH:mm"];
    }
}


+ (NSString *)getTimeDiffString:(NSTimeInterval)timestamp {
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDate *todate = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSDate *today = [NSDate date]; //当前时间
	unsigned int unitFlag = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
	NSDateComponents *gap = [cal components:unitFlag fromDate:today toDate:todate options:0]; //计算时间差

	if (ABS([gap day]) > 0) {
		return [NSString stringWithFormat:@"%ld天前", (long)(ABS([gap day]))];
	}
	else if (ABS([gap hour]) > 0) {
		return [NSString stringWithFormat:@"%ld小时前", (long)(ABS([gap hour]))];
	}
	else {
		return [NSString stringWithFormat:@"%ld分钟前",  (long)(ABS([gap minute]))];
	}
}

+(NSTimeInterval)formatTimeSinceNow:(NSTimeInterval)timestamp{
    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:timestamp];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
	NSString *thisDayMin = [dateFormatter stringFromDate:time];

    return [[dateFormatter dateFromString:thisDayMin] timeIntervalSince1970];
}

+ (NSString *)formatDateSinceNow:(NSTimeInterval)timestamp formatWith:(NSString *)format{
    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:timestamp];
    return [self formatDate:time formatWith:format];
}

+ (NSString *)formatTime:(NSTimeInterval)timestamp formatWith:(NSString *)format{

    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timestamp];
    if(time == 0){
        return @"";
    }else{
        return [self formatDate:time formatWith:format];
    }
}

//format :yyyy-MM-dd
+ (NSString *)formatDate:(NSDate *)date formatWith:(NSString *)format{
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:format];
	NSString *currentDateStr = [dateFormatter stringFromDate:date];
	return currentDateStr;
}

/**
 *	获取时间的key 按星期  e.q "从 2013-08-05 至 2013-08-11"
 *
 *	@param	timestamp
 *
 *	@return
 */
+ (NSString *)getWeekKeyString:(NSTimeInterval)timestamp {
	NSTimeInterval timestampStart = [self getFirstDayOfWeek:timestamp];
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestampStart];
	NSString *timeStart = [self formatDate:date formatWith:@"yyyy-MM-dd"];

	NSString *timeEnd = [self formatDate:[date dateByAddingTimeInterval:3600 * 24 * 6] formatWith:@"yyyy-MM-dd"];

	return [NSString stringWithFormat:@"From %@ to %@", timeStart, timeEnd];
}

+ (NSString *)getFirstDayForWeekKeyString:(NSTimeInterval)timestamp {
	NSTimeInterval timestampStart = [self getFirstDayOfWeek:timestamp];
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestampStart];
	NSString *timeStart = [self formatDate:date formatWith:@"yyyy-MM-dd"];

	return timeStart;
}

+ (NSString *)getMonthKeyStringByOffset:(NSInteger)month {
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:[NSDate date]];

	comps.month = comps.month + month;
	if (comps.month > 12) {
		comps.year = comps.year + 1;
		comps.month =  comps.month - 12;
	}
	else if (comps.month < 1) {
		comps.year = comps.year - 1;
		comps.month = comps.month + 12;
	}

	NSDate *date = [cal dateFromComponents:comps];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM"];
	NSString *currentDateStr = [dateFormatter stringFromDate:date];
	return currentDateStr;
}

+ (NSInteger)getTotalDayInMonth:(NSTimeInterval)timestamp{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate dateWithTimeIntervalSince1970:timestamp]];
    return range.length;
}


+ (NSTimeInterval)getFirstDayOfQuarter:(NSTimeInterval)timestamp {
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
	if (comps.month <= 3) {
		comps.month =  1;
	}
	else if (comps.month <= 6) {
		comps.month =  4;
	}
	else if (comps.month <= 9) {
		comps.month =  7;
	}
	else if (comps.month <= 12) {
		comps.month =  10;
	}

	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

+ (NSTimeInterval)getFirstDayOfWeek:(NSTimeInterval)timestamp {
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday| NSCalendarUnitWeekOfMonth
                               fromDate:now];
	NSLog(@"%ld,%ld,%ld,%ld", (long)comps.year, (long)comps.month, (long)comps.weekOfMonth, (long)comps.weekday);
	if (comps.weekday == 1) {
        return timestamp - 6*24*60*60;
    }else {
        return timestamp - (comps.weekday - 2)*24*60*60;
    }
}

+ (NSString *)getWeekTime
{
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
    NSLog(@"%ld----%ld",(long)weekDay,(long)day);
    
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1)
    {
        firstDiff = -6;
        lastDiff = 0;
    }
    else
    {
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
    NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit  fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    
    NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    
    NSString *dateStr = [NSString stringWithFormat:@"%lf-%lf",firstDayOfWeek.timeIntervalSince1970,lastDayOfWeek.timeIntervalSince1970];
    
    return dateStr;
    
}

+ (NSTimeInterval)getEndDayOfWeekWithFirstWeek:(NSTimeInterval)timestamp {
    
   return [self getFirstDayOfWeek:timestamp] + 24*60*60*7-1;
}

+(NSTimeInterval)getTodayStartDay
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:[NSDate date]];
    
    NSInteger H = components.hour;
    NSInteger Min = components.minute;
    NSInteger Sec = components.second;
    NSDate * date = [NSDate dateWithTimeIntervalSinceNow:-(Sec + Min*60+ H*3600)];
    return date.timeIntervalSince1970;
}

+(NSTimeInterval)getTodayEndDayWithStartDay:(NSTimeInterval)time
{
    return time + 3600*24 - 1;
}

+ (NSTimeInterval)getFirstDayOfMonth:(NSTimeInterval)timestamp {
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

+ (NSTimeInterval)getFirstDayOfLastMonth:(NSTimeInterval)timestamp{
	NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
	NSCalendar *cal = [NSCalendar currentCalendar];
	NSDateComponents *comps = [cal
	                           components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    if(comps.month - 1 == 0 ){
        comps.month = 12;
        comps.year = comps.year -1;
    }else{
        comps.month = comps.month - 1;
    }
	comps.day = 1;
	NSDate *firstDay = [cal dateFromComponents:comps];
	return [firstDay timeIntervalSince1970];
}

+ (NSTimeInterval)getFirstDayOfNextMonth:(NSTimeInterval)timestamp
{
    NSDate *now = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    if(comps.month + 1 == 13 ){
        comps.month = 1;
        comps.year = comps.year +1;
    }else{
        comps.month = comps.month + 1;
    }
    comps.day = 1;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970];

}

//本年第一天
+(NSTimeInterval)getFirstDayOfYear
{
    NSDate *now = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.month = 1;
    comps.day = 1;
    comps.hour = 0;
    comps.minute =0;
    comps.second =0;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970];
    
}

//上一年第一天
+(NSTimeInterval)getFirstDayOfNextYear
{
    NSDate *now = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal
                               components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                               fromDate:now];
    comps.year += 1;
    comps.month = 1;
    comps.day = 1;
    comps.hour = 0;
    comps.minute =0;
    comps.second =0;
    NSDate *firstDay = [cal dateFromComponents:comps];
    return [firstDay timeIntervalSince1970];

}




@end
